// generated 2005/8/8 17:10:19 CST by xihels@163.com
//

#if defined __GNUC__ && __GNUC__ < 3
#error This program will crash if compiled with g++ 2.x
// see the dynamic_cast bug in the gtkmm FAQ
#endif //
#include "config.h"
#include <gtkmmconfig.h>
#if GTKMM_MAJOR_VERSION==2 && GTKMM_MINOR_VERSION>2
#define GMM_GTKMM_22_24(a,b) b
#else //gtkmm 2.2
#define GMM_GTKMM_22_24(a,b) a
#endif //
#include <gtkmm/label.h>
#include <gtkmm/image.h>
#include <gdkmm/pixbufloader.h>
#include <gdk/gdkkeysyms.h>
#include <gtkmm/accelgroup.h>
#include <gtkmm/box.h>
#include <gtkmm/alignment.h>
#include <gtkmm/toolbutton.h>
#include <gtkmm/toolbar.h>
#include <gtkmm/buttonbox.h>
#include "pixmaps.h"
#include "ChatLogWin.h"
#include "config.h"
#include "CommandBar.h"
#include "MsgWindow.h"
#include "Smiley.h"
extern Smiley* g_smiley_wnd;

CommandBar::CommandBar(MsgWindow* _parent, GlademmData *gmm_data) :
    parent(reinterpret_cast<MsgWindow*>(_parent)),
    smileyWnd(NULL)
{
    CommandBar* commandBar = this;
    logoffset[0]=60*60*24*2;//2 days ago
    logoffset[1]=60*60*24*7;//1 week ago
    logoffset[2]=60*60*24*14;//2 week ago
    logoffset[3]=60*60*24*30;//1 month ago
    logoffset[4]=60*60*24*365;//for test
    logoffset_index=0;
    Gtk::Image *toolbuttonSmiley_img = getImage("insert-smiley-small.png");

    class Gtk::ToolButton * toolbuttonSmiley = 
	Gtk::manage(new class Gtk::ToolButton(*toolbuttonSmiley_img, ""));

    Gtk::Toolbar *msgAttrArch = Gtk::manage(new class Gtk::Toolbar());

    msgAttrArch->append(*toolbuttonSmiley);

    toolbuttonSmiley->set_visible_horizontal(true);
    toolbuttonSmiley->set_visible_vertical(true);
    toolbuttonSmiley->set_is_important(true);
    toolbuttonSmiley->set_homogeneous(true);
    toolbuttonSmiley->set_expand(false);

    msgAttrArch->set_tooltips(true);
    msgAttrArch->set_toolbar_style(Gtk::TOOLBAR_ICONS);
    msgAttrArch->set_orientation(Gtk::ORIENTATION_HORIZONTAL);
    msgAttrArch->set_show_arrow(true);

//////////////////////////////////////////////
    Gtk::Image *imageButtonLog =
	getImage("button_log.png");
    Gtk::Label *labelButtonLog = 
	Gtk::manage(new class Gtk::Label("日志(_L)", true));
    Gtk::HBox *hboxButtonLog =
	Gtk::manage(new class Gtk::HBox(false, 2));
    Gtk::Alignment *alignmentButtonLog = 
	Gtk::manage(new class Gtk::Alignment(0.5, 0.5, 0, 0));
    buttonLog = Gtk::manage(new class Gtk::Button());
/////////////////////////////////////////////
    Gtk::Image *imageButtonSend =
	getImage("button_send.png");
    Gtk::Label *labelButtonSend =
	Gtk::manage(new class Gtk::Label("发送(_S)", true));
    Gtk::HBox *hboxButtonSend =
	Gtk::manage(new class Gtk::HBox(false, 2));
    Gtk::Alignment *alignmentButtonSend =
	Gtk::manage(new class Gtk::Alignment(0.5, 0.5, 0, 0));
    buttonSend = Gtk::manage(new class Gtk::Button());
//////////////////////////////////////////// 
    Gtk::Image *imageButtonSave =
        getImage("button_save.png");
    Gtk::Label *labelButtonSave =
        Gtk::manage(new class Gtk::Label("保存(_T)",true));
    Gtk::HBox *hboxButtonSave =
    	Gtk::manage(new class Gtk::HBox (false,2));
    Gtk::Alignment *alignmentButtonSave =
	Gtk::manage(new class Gtk::Alignment(0.5,0.5,0,0));
    buttonSave= Gtk::manage(new class Gtk::Button());
//////////////////////////////////////////////    
    Gtk::HBox *HBoxComboboxDays=
    	Gtk::manage(new class Gtk::HBox(false,2));
    comboboxDays= Gtk::manage(new class Gtk::ComboBoxText());
    comboboxDays->append_text("两天前");
    comboboxDays->append_text("一周前");
    comboboxDays->append_text("两周前");
    comboboxDays->append_text("一个月以前");
    comboboxDays->append_text("显示所有");
    comboboxDays->set_active_text("两天前");
/////////////////////////////////////////
    Gtk::HButtonBox *commandArea = 
	Gtk::manage(new class Gtk::HButtonBox(Gtk::BUTTONBOX_END, 0));

/////////////////////////////////////////
    hboxButtonLog->pack_start(*imageButtonLog, Gtk::PACK_SHRINK, 0);
    hboxButtonLog->pack_start(*labelButtonLog, Gtk::PACK_SHRINK, 0);
    alignmentButtonLog->add(*hboxButtonLog);
    buttonLog->unset_flags(Gtk::CAN_FOCUS);
    buttonLog->set_relief(Gtk::RELIEF_NONE);
    buttonLog->add(*alignmentButtonLog);
////////////////////////////////////////
//printf("start to add button save!\n");
    hboxButtonSave->pack_start(*imageButtonSave, Gtk::PACK_SHRINK, 0);
    hboxButtonSave->pack_start(*labelButtonSave, Gtk::PACK_SHRINK, 0);
    alignmentButtonSave->add(*hboxButtonSave);
    buttonSave->unset_flags(Gtk::CAN_FOCUS);
    buttonSave->set_relief(Gtk::RELIEF_NONE);
    buttonSave->add(*alignmentButtonSave);
//printf("add button save ended!\n");
////////////////////////////////////////
    hboxButtonSend->pack_start(*imageButtonSend, Gtk::PACK_SHRINK, 0);
    hboxButtonSend->pack_start(*labelButtonSend, Gtk::PACK_SHRINK, 0);
    alignmentButtonSend->add(*hboxButtonSend);
    buttonSend->unset_flags(Gtk::CAN_FOCUS);
    buttonSend->set_relief(Gtk::RELIEF_NONE);
    buttonSend->add(*alignmentButtonSend);
////////////////////////////////////////
    HBoxComboboxDays->pack_start(*comboboxDays,Gtk::PACK_SHRINK,0);

////////////////////////////////////////
    buttonHideMember = Gtk::manage(new class Gtk::Button("_H", true));
    buttonHideMember->unset_flags(Gtk::CAN_FOCUS);
    buttonHideMember->set_relief(Gtk::RELIEF_NONE);
////////////////////////////////////////

    commandArea->pack_start(*buttonLog);
    commandArea->pack_start(*HBoxComboboxDays);
    commandArea->pack_start(*buttonSave);
    commandArea->pack_start(*buttonSend);
    commandBar->pack_start(*msgAttrArch);
    commandBar->pack_start(*commandArea, Gtk::PACK_SHRINK, 0);
    commandBar->pack_start(*buttonHideMember, Gtk::PACK_SHRINK, 0);

    commandBar->show_all();

    buttonSend->signal_clicked().connect(
	    sigc::mem_fun(*this, &CommandBar::on_buttonSend_clicked), false);
    buttonSave->signal_clicked().connect(
	    sigc::mem_fun(*this, &CommandBar::on_buttonSave_clicked), false);
    buttonLog->signal_clicked().connect(
	    sigc::mem_fun(*this, &CommandBar::on_buttonLog_clicked), false);
    toolbuttonSmiley->signal_clicked().connect(
	    sigc::mem_fun(*this, &CommandBar::on_toolbuttonSmiley_clicked), false);
    buttonHideMember->signal_clicked().connect(
	    sigc::mem_fun(*this, &CommandBar::on_buttonHideMember_clicked), false);
    comboboxDays->signal_changed().connect( sigc::mem_fun(*this, &CommandBar::on_comboboxDays_changed) );
}

CommandBar::~CommandBar()
{
    delete smileyWnd;
}
void CommandBar::on_comboboxDays_changed()
{
    logoffset_index=comboboxDays->get_active_row_number();
    //cout<<comboboxDays->get_active_text()<<endl;
}
void CommandBar::on_buttonSend_clicked()
{
    parent->sendMessage();
}
void CommandBar::on_buttonSave_clicked()
{
    parent->saveLog();
}
void CommandBar::on_buttonLog_clicked()
{
    parent->showLog(logoffset[logoffset_index]);
    
}
void CommandBar::on_buttonHideMember_clicked()
{
    parent->hideMemberList();
}
void CommandBar::on_toolbuttonSmiley_clicked()
{
    if (NULL == smileyWnd)
	smileyWnd = new Smiley();
    smileyWnd->showWindow(parent->getCurrentPage());
}
